<template>
  <div class="app-container">
    <div style="width:100%; text-align: right;">
      <el-button size="small" type="primary" icon="el-icon-download" @click="exportData">导出</el-button>
    </div>
    <div style="width: 100%; margin-top: 8px;">
      <el-form :model="searchForm" label-width="80px">
        <div style="display: flex;">
          <el-form-item label="车牌号">
            <el-input v-model="searchForm.searchValue" size="small" placeholder="车牌号 | 驾驶员 | 手机号" style="width: 200px;"></el-input>
          </el-form-item>
          <el-form-item label="开始时间" style="margin-left: 10px;">
            <el-date-picker
                v-model="searchForm.searchBeginDate"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择开始时间"
                align="right"
                :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间" style="margin-left: 10px;">
            <el-date-picker
                style="display: inline-block;"
                v-model="searchForm.searchEndDate"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择结束时间"
                align="right"
                :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
          <el-button-group style="margin-left: 16px;">
            <el-button type="primary" plain icon="el-icon-search" @click="search">搜索</el-button>
            <el-button @click="refresh">重置</el-button>
          </el-button-group>
        </div>
      </el-form>
    </div>

    <el-divider />

    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" stripe>
      <el-table-column type="index" label="序号" width="50" />
      <el-table-column label="车牌号" prop="carNo" />
      <el-table-column label="时间" prop="date" />
      <el-table-column label="驾驶员姓名" prop="driverName" />
      <el-table-column label="驾驶员电话" prop="driverPhone" />
      <el-table-column label="方向">
        <template slot-scope="scope">
          {{ scope.row.direction === 0 ? '入场' : '出场'  }}
        </template>
      </el-table-column>
      <el-table-column label="出入口" prop="gateName" />
      <el-table-column fixed="right" label="操作" width="80" align="center">
        <template slot-scope="scope">
          <span>
            <el-button plain  circle type="success" icon="el-icon-more" size="mini" @click="detail(scope.row)" title="详情"></el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="searchForm.pageNum" :limit.sync="searchForm.pageSize"
      @pagination="fetchData" style="text-align: center"/>

    <el-dialog :visible.sync="dialogVisible" title="详情">
      <el-form ref="dataForm" :model="detailData" label-width="120px" label-position="right" v-if="detailData">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="车牌号">
              <span>{{detailData.carNo}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发生时间">
              <span>{{detailData.date}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出入口">
              <span>{{detailData.gateName}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="方向">
              <span> {{ detailData.direction === 0 ? '入场' : '出场' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="驾驶员姓名">
              <span>{{detailData.driverName}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="驾驶员手机">
              <span>{{detailData.driverPhone}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号">
              <span>{{detailData.driverIdcard}}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  import Pagination from '@/components/Pagination'
  import {
    inOutList,
    inOutExport, carExport
  } from '@/api/short'
  import {
    deepClone
  } from '@/utils'

  export default {
    components: {
      Pagination
    },
    data() {
      return {
        total: 0,
        list: [],
        listLoading: false,
        searchForm: {
          pageNum: 1,
          pageSize: 10,
          searchValue: '',
          searchBeginDate: '',
          searchEndDate: '',
        },
        uploadUrl: '',
        dialogVisible: false,
        loading: false,
        pickerOptions: {
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
        detailData: null
      }
    },
    mounted() {
      this.fetchData()
    },
    methods: {
      exportData() {
        let queryParams = {
          searchValue: this.searchForm.searchValue,
          searchBeginDate: this.searchForm.searchBeginDate,
          searchEndDate: this.searchForm.searchEndDate,
        }
        inOutExport(queryParams).then(response => {
          if(response.code === 200) {
            let filename = response.msg
            let url = '/download/' + filename
            let link = document.createElement("a");
            link.style.display = "none";
            link.href = url;
            link.setAttribute("download", filename); //指定下载后的文件名，防跳转
            document.body.appendChild(link);
            link.click();
          }
        })
      },
      detail(row) {
        this.detailData = row
        this.dialogVisible = true
      },
      search() {
        this.fetchData()
      },
      refresh() {
        this.searchForm = {
          pageNum: 1,
          pageSize: 10,
          searchValue: '',
          searchBeginDate: '',
          searchEndDate: '',
        }
        this.fetchData()
      },
      fetchData() {
        this.listLoading = true
        inOutList(this.searchForm).then(response => {
          this.list = response.rows
          this.total = response.total
          this.listLoading = false
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .el-upload {
    border: 1px dashed #d9d9d9 !important;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;

    .el-icon-plus.avatar-uploader-icon {
      border: 1px dashed #d9d9d9 !important;
      border-radius: 6px;
      font-size: 28px;
      color: #8c939d;
      width: 128px;
      height: 128px;
      line-height: 128px;
      text-align: center;
    }
  }

  .avatar-uploader {
    height: 128px;

    img {
      width: 128px;
      height: 128px;
    }
  }
</style>
